package com.epam.kulibaba.helpers.testSupport;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.Reporter;

import com.epam.kulibaba.components.PanelCriterias.PanelCornerCriterias;
import com.epam.kulibaba.helpers.Class.Catalog;
import com.epam.kulibaba.helpers.Class.Product;

public class PanelCriteriasHelper {

	public static void setMinPrice(PanelCornerCriterias minPrices,
			double minPrice) {
		Reporter.log("set min price<br>");
		List<WebElement> itemPrices = minPrices.getMinPrices().findElements(
				By.cssSelector("a"));
		for (WebElement price : itemPrices) {
			if (Double.parseDouble(price.getText()) == minPrice) {
				price.click();
				break;
			}
		}
	}

	public static void setMaxPrice(PanelCornerCriterias maxPrices,
			double maxPrice) {
		Reporter.log("set max price<br>");
		List<WebElement> itemPrices = maxPrices.getMaxPrices().findElements(
				By.cssSelector("a"));
		for (WebElement price : itemPrices) {
			if (Double.parseDouble(price.getText()) == maxPrice) {
				price.click();
				break;
			}
		}
	}

	public static void showAllProducers(
			PanelCornerCriterias linkShowAllProducers) {
		Reporter.log("show all producers<br>");
		linkShowAllProducers.getLinkShowAllProducers().click();
	}

	public static HashSet<String> getAllProductsProducers(
			PanelCornerCriterias linkShowAllProducers,
			PanelCornerCriterias ListOfproducers) {
		HashSet<String> setProducers = new HashSet<String>();
		PanelCriteriasHelper.showAllProducers(linkShowAllProducers);
		List<WebElement> otherNameProdicers = ListOfproducers
				.getListOfproducers().findElements(By.cssSelector("a.active"));
		for (WebElement name : otherNameProdicers) {
			setProducers.add(name.getText().toLowerCase());
		}
		// System.out.println("HashSet: \t" + setProducers.toString() + " (" +
		// setProducers.size() + ")");
		return setProducers;
	}

	public static List<String> getProducersAllProductsList(Catalog catalog) {
		List<Product> myProducts = new ArrayList<Product>(catalog.globals);
		List<String> nameProducers = new ArrayList<String>();
		for (Product item : myProducts) {
			String name = item.getName().toLowerCase();
			nameProducers.add(name.substring(0, name.indexOf(" ")));
		} // System.out.println(nameProducers);
		return nameProducers;
	}

	public static void checkProducersFilterTest(HashSet<String> setProducers,
			List<String> nameProducers) {
		Assert.assertEquals(setProducers.containsAll(nameProducers), true);
	}

	public static void setFunctionWeightAjustment(
			PanelCornerCriterias weightAdjustment) {
		weightAdjustment.getWeightAdjustment().click();
	}

	public static void checkSetFunction(PanelCornerCriterias weightAdjustment, List<String> shortDescription) {
		String function = weightAdjustment.getWeightAdjustment().getText().toLowerCase();
		System.out.println(function);
		for (String desc : shortDescription) {
			System.out.println(" - "+desc);
			Assert.assertEquals(desc.contains(function), true);
		}
	}
}
